/*
 *  Others.cpp
 *  T3nsors
 *
 *  Created by Michael Barriault on 10-06-07.
 *  Copyright 2010 University of Guelph. All rights reserved.
 *
 */

#include "Others.h"
#include "Macros.h"
#include <cstdlib>
#include <sys/time.h>
#include <iostream>
#include <sstream>
#include <cmath>
using namespace std;

string itos(long long i) {
	ostringstream str;
	str << i;
	return str.str();
}

string ftos(double i) {
	ostringstream str;
	str << i;
	return str.str();
}

int stoi(string str) {
	std::istringstream istr(str);
	int i;
	istr >> i;
	return i;
}

double stof(string str) {
	std::istringstream istr(str);
	double i;
	istr >> i;
	return i;
}

void Alloc_Error(string id) {
	std::cout << "Allocation error making " << id << std::endl;
}

void print(string str, bool fl) {
#ifdef DEBUG
	std::cout << str << std::flush;
	if ( not fl ) std::cout << std::endl;
#endif
}

string timer(struct timeval before, int mult) {
	std::ostringstream str;
	struct timezone tzp;
	struct timeval now,lapsed;
	gettimeofday(&now,&tzp);
	if ( before.tv_usec > now.tv_usec ) {
		now.tv_usec += 1000000;
		now.tv_sec--;
	}
	lapsed.tv_usec = now.tv_usec - before.tv_usec; 
	lapsed.tv_sec = now.tv_sec - before.tv_sec; 
	int left = int(mult*(lapsed.tv_sec+double(lapsed.tv_usec)/1E6));
	if ( left > 3600 )
		str << left/3600 << "h ";
	if ( left > 60 )
		str << (left%3600)/60 << "m ";
	str << left%60 << "s" << std::flush;
	return str.str();
}

string bytes(long long int b) {
	char pre[10] = "BKMGTPEZY";
	int i=0;
	string rd = "";
	int rem;
	do {
		rem = b%1024;
		rd = itos(rem) + pre[i] + " " + rd;
		b = (b - rem)/1024;
		i++;
	} while ( b > 0);
	return rd;
}

double smoother(double x, double x0) {
	return pow(pow(x,2)-pow(x0,2),4);
}

void args(int argc, char*const argv[], int *n, double *epsilon, double *K, double *J, double *c, double *s, double *T, int *p, int *damp, int *icid, double *w, int *all) {
	for ( int i=1; i<argc; i++ ) switch ( argv[i][0] ) {
		case 'n':
			*n = stoi(((string)argv[i]).erase(0,1));
			break;
		case 'e':
			*epsilon = stof(((string)argv[i]).erase(0,1));
			break;
		case 'k':
			*K = stof(((string)argv[i]).erase(0,1));
			break;
		case 'c':
			*c = stof(((string)argv[i]).erase(0,1));
			break;
		case 's':
			*s = stof(((string)argv[i]).erase(0,1));
			break;
		case 'T':
			*T = stof(((string)argv[i]).erase(0,1));
			break;
		case 'j':
			*J = stof(((string)argv[i]).erase(0,1));
			break;
		case 'p':
			FORa p[a] = int(argv[i][a+1]-48);
			FORa if ( p[a] > 1 ) p[a] = 1;
			break;
		case 'd':
			*damp = stoi(((string)argv[i]).erase(0,1));
			break;
		case 'a':
			*all = stoi(((string)argv[i]).erase(0,1));
			break;
		case 'i':
			*icid = stoi(((string)argv[i]).erase(0,1));
			break;
		case 'w':
			*w = stof(((string)argv[i]).erase(0,1));
		case 'h':
			cout << "n = " << *n << endl;
			cout << "e = " << *epsilon << endl;
			cout << "k = " << *K << endl;
			cout << "c = " << *c << endl;
			cout << "w = " << *w << endl;
			cout << "s = " << *s << endl;
			cout << "T = " << *T << endl;
			cout << "j = " << *J << endl;
			cout << "damp = " << *damp << endl;
			cout << "p = ";
			FORa cout << p[a] << ",";
			cout << endl;
			exit(0);
	}
}
